using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统.Sec_Tools
{
    public partial class Sec_MysqlClient : Form
    {
        public Sec_MysqlClient()
        {
            InitializeComponent();
        }

        private void pictureBox11_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Sec_MysqlClient_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("数据不能为空！");
                return;
            }
            else
            {
                MysqlOpen();
            }
        }
        public static MySqlConnectionStringBuilder mysql = new MySqlConnectionStringBuilder();
        public static MySqlConnection connection = new MySqlConnection(mysql.ToString());
        public void MysqlOpen()
        {
            //mySqlConnection mysql = new MySqlConnectionStringBuilder();
            mysql.Server = textBox1.Text;
            mysql.Port = Convert.ToUInt32(textBox2.Text);
            mysql.UserID = textBox3.Text;
            mysql.Password = textBox4.Text;
            mysql.Database = textBox5.Text;
            MySqlConnection connection = new MySqlConnection(mysql.ToString());
            try
            {
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    button1.Enabled = false;
                    button2.Enabled = true;
                    textBox1.Enabled = false;
                    textBox4.Enabled = false;
                    textBox5.Enabled = false;
                    button3.Enabled = true;
                    MessageBox.Show("数据库连接成功！");
                }
                else
                {
                    MessageBox.Show("数据库无法打开或密码错误！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void MysqlClose()
        {
            if(connection.State == ConnectionState.Open)
            {
                connection.Close();
                MessageBox.Show("数据库已关闭");
                button1.Enabled = true;
                button2.Enabled = false;
                textBox1.Enabled = true;
                textBox4.Enabled = true;
                textBox5.Enabled = true;
                button3.Enabled = false;
            }
            else
            {
                MessageBox.Show("数据库已关闭");
                button1.Enabled = true;
                button2.Enabled = false;
                textBox1.Enabled = true;
                textBox4.Enabled = true;
                textBox5.Enabled = true;
                button3.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MysqlClose();
        }
        public int oldX, oldY;
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标左键按下时的位置
                this.oldX = e.Location.X;
                this.oldY = e.Location.Y;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(textBox1.Enabled == true)
            {
                MessageBox.Show("请连接数据库后进行操作！");
                return;
            }
            try
            {
                richTextBox1.Clear();
                mysql.Server = textBox1.Text;
                mysql.Port = Convert.ToUInt32(textBox2.Text);
                mysql.UserID = textBox3.Text;
                mysql.Password = textBox4.Text;
                mysql.Database = textBox5.Text;
                MySqlConnection connection = new MySqlConnection(mysql.ToString());
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = comboBox1.Text;
                    MySqlDataReader myReader = command.ExecuteReader();
                    myReader.Read();
                    Console.WriteLine(richTextBox1.Text += "执行时间：" + DateTime.Now.ToString() + "\r\n" + "执行状态：" + "成功 " + "\r\n" + "执行结果：" + "\r\n" + myReader.GetValue(0));
                }
                else
                {
                    MessageBox.Show("无法执行命令，数据库关闭，请连接数据库！");
                }
            }
            catch(Exception)
            {
                MessageBox.Show("Sql语法错误或数据库无法连接！");
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //计算鼠标移动距离
                this.Left += e.Location.X - this.oldX;
                this.Top += e.Location.Y - this.oldY;
            }
        }
    }
}
